import axios from 'axios'
export default {
    state() {
        return {

            list: []


        }
    },
    mutations: {
        changeList(state, list) {
            state.list = list
        },
        updateCount(state, obj) {
            var itemObj = state.list.find((item) => item.id == obj.id)
            itemObj.count = obj.count
                // console.log(state.list)
        }

    },
    actions: {
        async getList({ commit }) {
            const res = await axios('http://localhost:3000/cart')
            console.log(res);
            commit('changeList', res.data)
        },
        async getCount({ commit }, obj) {
            const res = await axios.patch('http://localhost:3000/cart/' + obj.id, {
                count: obj.count
            })
            commit('updateCount', obj)
        }
    },
    getters: {
        //底部总数量展示
        total(state) {
            return state.list.reduce((sum, item) => sum + item.count, 0)

        },
        totalPrice(state) {
            return state.list.reduce((sum, item) => sum + item.count * item.price, 0)

        }
    },
    namespaced: true
}